package com.frost.trans.controller;

import com.frost.trans.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/trans")
public class TransactionController {

    @Autowired
    private UserInfoService userInfoService;

    @Transactional // 事务
    @RequestMapping("/r1")
    public String r1(String userName, String password) {

        Integer result = userInfoService.insertUser(userName, password);
        int a = 10 / 0;
        return "" + result;
    }

    @Transactional
    @RequestMapping("/r2")
    public String r2(String userName, String password) {

        Integer result = userInfoService.insertUser(userName, password);
        if (true) {
            /**
             * 运行时异常，@Transactional只会对运行时异常和ERROR进行回滚
             */
            throw new NullPointerException(); // 事务进行了回滚
        }

        return "" + result;
    }

    @Transactional
    @RequestMapping("/r3")
    public String r3(String userName, String password) throws IOException {

        Integer result = userInfoService.insertUser(userName, password);
        if (true) {
            /**
             * 非运行时异常，@Transactional不会对它进行回滚
             */
            throw new IOException(); // 事务进行了提交
        }

        return "" + result;
    }

    @Transactional(rollbackFor = {Exception.class})
    @RequestMapping("/r4")
    public String r4(String userName, String password) throws IOException {

        Integer result = userInfoService.insertUser(userName, password);
        if (true) {
            /**
             * 现在会回滚
             */
            throw new IOException(); // 事务进行了回滚
        }

        return "" + result;
    }

    @Transactional
    @RequestMapping("/r5")
    public String r5(String userName, String password) {
        // 异常不抛出,进行catch,事务提交
        Integer result = userInfoService.insertUser(userName, password);
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.info("发生异常");
        }

        return "" + result;
    }

    @Transactional
    @RequestMapping("/r6")
    public String r6(String userName, String password) {
        Integer result = userInfoService.insertUser(userName, password);
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.info("发生异常");
            throw e; // 虽然捕获了异常但是继续抛出,此时事务回滚
        }

        return "" + result;
    }

    @Transactional(isolation = Isolation.DEFAULT)
    @RequestMapping("/r7")
    public String r7(String userName, String password) {
        Integer result = userInfoService.insertUser(userName, password);
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.info("发生异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); // 手动设置回滚
        }

        return "" + result;
    }
}
